define(['jquery','api','user','util'],function($,api,user,util){
	var vm,
	   channels=[],
	   wxid,
	   controller = {
	   init:function(){
	   	   if(!user.checkLogin())return;

			wxid=api.getWxid();
		   api.setTitle(L('html_title_bank_list'));
		   	api.headerShow(1);
            api.headerTitle(L('html_title_bank_list'));
            api.headerLeftLink('#/user');
		   
		   this.render();
	   },
	   render:function(){
	   		var that=this;
	   		$('#common').html('<div id="bank"></div>');
	       require(['text!pages/bank/list.html','Vue'],function(tpl,Vue){
	            vm=new Vue({
	               el:'#bank',
	               data:{
	               	  show:true,
	               	  channels:channels,
		              channelId:'',
		              pwd:'',
		              showPanel:false,
		              loading:true,
		              wxid:wxid,
		              noRecord:'/static/img/none_content.png'
	               },
	               methods:{
	               	resetForm:function(){
	               		this.channels = [];
	               		this.channelId = '';
	               		this.pwd = '';
	               	},
	                  selectChannel:function(channelId){
	              		this.channelId=channelId;
	              	},
	              	showUnbindPanel:function(){
	              		this.showPanel=true;
	              	},
	              	cancelPanel:function(){
	              		this.showPanel=false;
	              	},
	              	bind:function(){
	              		//跳转通道选择页面
//	              		location.hash='#/fund/bind/'+66+'/'+encodeURIComponent(location.href);
	              		location.hash='#/bank/channel';
	              	},
	              	doUnbind:function(){
//	              		var data={
//	              		   channelid:this.channelId,
//	              		   moneypwd:this.pwd
//	              		};
//	              		if (this.pwd == ''){
//			                util.msg('输入支付密码！');
//			                $("#pwd").focus();
//			                return false;
//			            }

	              		api.unBind({wxid:vm.wxid,channelid:this.channelId},function(rs){
	              		    if(rs.retcode=='0'){
	              		        util.msg('解绑成功！');
	              		        vm.resetForm();
	              		        vm.cancelPanel();
	              		        setTimeout(function(){that.loadChannel();},1000);
	              		    }else if(rs.retcode=='-4040'){
	              		        util.msg('密码错误！');
	              		    }else{
	              		        util.dealErrorCode(rs.retcode);
	              		    }
	              		});
	              	}
	               },
	               template:tpl
	            })
	      });
	      
	      that.loadChannel();
	   },
	   loadChannel:function(){
	   	   var channels=[],
	   	       channel;
   	       api.userInfo(function(rs){
   	       		vm.loading = false;
		   	   if(rs.retcode=='0' && rs.data){
	   	   			for(var i=0;i<rs.data.channel.length;i++){
	   	               channel=rs.data.channel[i];
	   	               if(channel.bankaccount!='')channels.push(channel);
	   	           }
	   	   			
	   	   			if(channels.length){
			   	       vm.channelId=channels[0].channelid;
			   	       vm.channels=channels;
		   	       }
		   	   }else{
		   	      util.msg('用户信息获取失败！');
		   	   }
		   });
	   },
	   onRouteChange:function(){
	       if(vm)vm.show=false;
	   }
	};
    return controller;
})